
package canglang.monitor.user.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import canglang.monitor.core.base.dao.BaseDAO;
import canglang.monitor.core.base.pagination.PaginatedListHelper;
import canglang.monitor.core.base.util.StringUtil;
import canglang.monitor.user.model.UserModel;
import canglang.monitor.user.to.SearchTO;

@SuppressWarnings("unchecked")
public class UserDAO extends BaseDAO {
	public  void addUser(UserModel userModel){
        String sql="addUser";
        Map map=objectToMap(userModel);
        getSqlMapClientTemplate().insert(sql,map);
    }
	
	public UserModel getUser(String userId){
        String sql="getUser";
        Map map=new HashMap();
        map.put("userId", userId);
        return (UserModel)getSqlMapClientTemplate().queryForObject(sql,map);
    }
	
    public void saveUser(UserModel userModel){
        String sql="saveUser";
        Map map=objectToMap(userModel);
        getSqlMapClientTemplate().update(sql,map);
    }	
	
    public void deleteUser(String userId){
        String sql="deleteUser";
        Map map=new HashMap();
        map.put("userId", userId);
        getSqlMapClientTemplate().delete(sql,map);
    }  	
		
    public Integer searchCount(SearchTO searchTO) {
        Map map = new HashMap();
        String userId=StringUtil.convertEmpty2Null(searchTO.getUserId());
        if(userId!=null)userId="%"+userId+"%";        
        String userName=StringUtil.convertEmpty2Null(searchTO.getUserName());
        if(userName!=null)userName="%"+userName+"%";
        String userRole=StringUtil.convertEmpty2Null(searchTO.getUserRole());
        map.put("userId", userId);
        map.put("userName", userName);
        map.put("userRole", userRole);
        return (Integer) getSqlMapClientTemplate().queryForObject("searchUserCount", map);
    }

    public List searchList(PaginatedListHelper helper, SearchTO searchTO) {
        Map map = new HashMap();
        String userId=StringUtil.convertEmpty2Null(searchTO.getUserId());
        if(userId!=null)userId="%"+userId+"%";
        String userName=StringUtil.convertEmpty2Null(searchTO.getUserName());
        if(userName!=null)userName="%"+userName+"%";
        String userRole=StringUtil.convertEmpty2Null(searchTO.getUserRole());
        map.put("userId", userId);
        map.put("userName", userName);
        map.put("userRole", userRole);
        map = constructPaginationMap(helper, map);
        return getSqlMapClientTemplate().queryForList("searchUserList", map);
    }

}
